package com.yhny.lykj.yuhannongye.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;

import com.longsichao.lscframe.ctrl.DataManager;
import com.longsichao.lscframe.model.LSCModel;
import com.longsichao.slideview.SlideView;
import com.yhny.lykj.yuhannongye.Adapter.ShopListAdapter;
import com.yhny.lykj.yuhannongye.Adapter.SlideViewAdapter;
import com.yhny.lykj.yuhannongye.R;
import com.yhny.lykj.yuhannongye.app.BaseFragment;
import com.yhny.lykj.yuhannongye.app.BaseRefreshListAdapter;
import com.yhny.lykj.yuhannongye.app.LSCGridLayoutManager;
import com.yhny.lykj.yuhannongye.app.MyRecyclerView;
import com.yhny.lykj.yuhannongye.app.MyScrollview;
import com.yhny.lykj.yuhannongye.model.BaseModel;
import com.yhny.lykj.yuhannongye.model.PosterListModel;
import com.yhny.lykj.yuhannongye.model.ShopListModel;
import com.yhny.lykj.yuhannongye.net.NetHelper;
import com.yhny.lykj.yuhannongye.tools.CodeScan;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by leach on 2017/7/26.
 */
public class HomeFragment extends BaseFragment {
    private SwipeRefreshLayout swipeRefreshLayout;
    private MyScrollview scrollView;

    private SlideView slideShowView;
    private ArrayList<PosterListModel.ListEntity> posterList;
    private SlideViewAdapter slideViewAdapter;

    private MyRecyclerView likeListView;
    private ShopListAdapter likeListAdapter;
    private List<ShopListModel.ListEntity> likeList;

    private RecyclerView hotListView;
    private ShopListAdapter hotListAdapter;
    private List<ShopListModel.ListEntity> hotList;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_home, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        swipeRefreshLayout = (SwipeRefreshLayout)view.findViewById(R.id.home_refrech);
        scrollView = (MyScrollview)view.findViewById(R.id.scroll);
        swipeRefreshLayout.setRefreshing(true);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                initData();
            }
        });
        scrollView.getViewTreeObserver().addOnScrollChangedListener(new  ViewTreeObserver.OnScrollChangedListener() {
            @Override
            public void onScrollChanged() {
                if (swipeRefreshLayout != null) {
                    if (scrollView.getScrollY()==0){
                        //顶部
                        swipeRefreshLayout.setEnabled(true);
                    }
                    if (scrollView.getChildAt(0).getHeight() - scrollView.getHeight() == scrollView.getScrollY()){
                        //底部
                    }
                }

            }
        });
        view.findViewById(R.id.left_group).setOnClickListener(this);
        view.findViewById(R.id.left_text).setOnClickListener(this);
        view.findViewById(R.id.right_group).setOnClickListener(this);
        view.findViewById(R.id.right_text).setOnClickListener(this);
        view.findViewById(R.id.type1).setOnClickListener(this);
        view.findViewById(R.id.type2).setOnClickListener(this);
        view.findViewById(R.id.type3).setOnClickListener(this);
        view.findViewById(R.id.type4).setOnClickListener(this);
        view.findViewById(R.id.name_top_bar).setOnClickListener(this);
        view.findViewById(R.id.search).setOnClickListener(this);
        view.findViewById(R.id.lable_1).setOnClickListener(this);
        view.findViewById(R.id.lable_2).setOnClickListener(this);
        view.findViewById(R.id.lable_3).setOnClickListener(this);

        slideShowView = (SlideView) view.findViewById(R.id.slideshowView);//轮播图
        slideShowView.getmViewPager().setOffscreenPageLimit(5);
        slideShowView.setHintColor(R.color.colorPrimary);
        posterList = new ArrayList<>();
        slideViewAdapter = new SlideViewAdapter(posterList);
        slideShowView.setAdapter(slideViewAdapter);

        //精选推荐
        likeListView = (MyRecyclerView) view.findViewById(R.id.main_like);
        final LSCGridLayoutManager layoutManager = new LSCGridLayoutManager(getActivity(),2);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        likeListView.setLayoutManager(layoutManager);
        likeListView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(getActivity()).sizeResId(R.dimen.item_vertical_margin_h).colorResId(R.color.white).build());
        likeList = new ArrayList<>();
        likeListAdapter = new ShopListAdapter(likeList);
        likeListAdapter.setOnItemClickListener(new BaseRefreshListAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Intent intent = new Intent(getActivity(),ShopListActivity.class);
                intent.putExtra(SHOP_FROM,SHOP_LIST);
                intent.putExtra(SHOP_ID,likeList.get(position).goodsid);
                startActivity(intent);
            }
        });
        likeListView.setAdapter(likeListAdapter);
        //精选每日
        hotListView = (RecyclerView) view.findViewById(R.id.home_day);
        final LSCGridLayoutManager layoutManager1 = new LSCGridLayoutManager(getActivity(),2);
        layoutManager1.setOrientation(LinearLayoutManager.VERTICAL);
        hotListView.setLayoutManager(layoutManager1);
        hotListView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(getActivity()).sizeResId(R.dimen.item_vertical_margin_h).colorResId(R.color.white).build());
        hotList = new ArrayList<>();
        hotListAdapter = new ShopListAdapter(hotList);
        hotListAdapter.setOnItemClickListener(new BaseRefreshListAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Intent intent = new Intent(getActivity(),ShopListActivity.class);
                intent.putExtra(SHOP_FROM,SHOP_LIST);
                intent.putExtra(SHOP_ID,hotList.get(position).goodsid);
                startActivity(intent);
            }
        });
        hotListView.setAdapter(hotListAdapter);

        initData();
    }

    private void initData(){
        //轮播图
        DataManager.getData(MODEL_PROCESS_2, NetHelper.Poster.getPosterList(1), this);
        DataManager.getData(MODEL_PROCESS_3, NetHelper.Shop.searchgoodcomlist(1), this);

        DataManager.getData(MODEL_PROCESS_1, NetHelper.Shop.searchgoodsdaylist(1), this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.left_group:
            case R.id.left_text:{
                //二维码
                CodeScan.startCodeScan(getActivity());
                break;
            }
            case R.id.right_group:
            case R.id.right_text:{
                //消息
                Intent intent = new Intent(getActivity(),MsgListActivity.class);
                startActivity(intent);
                break;
            }
            case R.id.type1:{
                //分类1
                MainActivity.mainActivity.setFragments(BOTTOM_SHOP);
                break;
            }
            case R.id.name_top_bar:
            case R.id.search:{
                //搜索
                Intent intent = new Intent(getActivity(),SearchActivity.class);
                startActivity(intent);
                break;
            }
            case R.id.type2:{
                //分类2
                MainActivity.mainActivity.setFragments(BOTTOM_SHOP);
                break;
            }
            case R.id.type3:{
                //分类3
                MainActivity.mainActivity.setFragments(BOTTOM_SHOP);
                break;
            }
            case R.id.type4:{
                //分类4
                MainActivity.mainActivity.setFragments(BOTTOM_SHOP);
                break;
            }
            case R.id.lable_1:{
                //广告1
                break;
            }
            case R.id.lable_2:{
                //广告2
                break;
            }
            case R.id.lable_3:{
                //广告3
                break;
            }
        }
    }

    @Override
    public boolean onModel(int caseId, LSCModel model) {
        if (super.onModel(caseId, model)) {
            dismissProgressDialog();
            return false;
        }
        switch (caseId){
            case MODEL_PROCESS_1: {
                //分类
                //商品列表
                BaseModel baseModel = (BaseModel) model;
                if (isDataEmpty(baseModel)) {

                } else {
                    ShopListModel modelDataList = (ShopListModel) model;
                    if (modelDataList == null || modelDataList.data == null || modelDataList.data.isEmpty()) {

                    } else {
                        likeList.clear();
                        likeList.addAll(modelDataList.data);
                        likeListAdapter.notifyDataSetChanged();
                    }
                }
                dismissProgressDialog();
                break;
            }
            case MODEL_PROCESS_2:{
                //轮播图
                PosterListModel posterListModel = (PosterListModel)model;
                if (!isDataEmpty(posterListModel)){
                    //设置图片
                    posterList.clear();
                    posterList.addAll(posterListModel.data);
                    slideViewAdapter.notifyDataSetChanged();
                }

                break;
            }
            case MODEL_PROCESS_3:{
                //猜你喜欢
                ShopListModel shopListModel = (ShopListModel)model;
                if (isDataEmpty(shopListModel)||shopListModel.data==null||shopListModel.data.isEmpty()) {
                } else {

                    hotList.clear();
                    hotList.addAll(shopListModel.data);
                    hotListAdapter.notifyDataSetChanged();
                }
                break;
            }
        }
        return false;
    }
}
